var app=getApp()
var modifyCase_url ="https://www.hkzhaoscut.top/doctor/addAuthCase"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    patientName: '', //病人姓名
    pAddress: ['广东省', '广州市', '番禺区'], //通讯地址
    pAge: '', //年龄
    pSex: '', //性别
    pPhone: '', //联系方式

    // degreeArr: [ '轻微', '一般', '严重', '紧急'], //病情程度的数组
    // degree: '轻微', //病情程度
    // degreeId: 0, //病情程度的选择器变量

    conditionArr: ['无症状', '轻症', '重症', '治愈', '死亡'], //当前状态的数组
    condition: '无症状', //当前状态
    conditionId: 0, //当前状态的选择器变量

    illness: '', //疾病类型
    // illDate: '2020-01-01', //发病时间
    // symptom: '', //发病症状
    remarks: '', //备注信息
    caseId: '',
    doctorTokenId : '',
  },
  // 联系地址选择器
  bindRegionChange: function(e) {
    // console.log('pAddress:', e.detail.value)
    this.setData({
      pAddress: e.detail.value
    })
  },
  // // 病情程度选择器
  // degreePicker(e) {
  //   var degreeId = e.detail.value
  //   var degreeArr = this.data.degreeArr
  //   var degree = degreeArr[degreeId]
  //   // console.log("degree:" + degree)
  //   this.setData({
  //     degreeId: degreeId,
  //     degree: degree

  //   })
  // },
  // 当前状态选择器
  conditionPicker(e) {
    var conditionId = e.detail.value
    var conditionArr = this.data.conditionArr
    var condition = conditionArr[conditionId]
    // console.log("conditon："+condition)
    this.setData({
      conditionId: conditionId,
      condition: condition
    })

  },
  // 发病时间选择器
  bindDateChange(e) {
    // console.log(e)
    var illDate = e.detail.value
    this.setData({
      illDate: illDate
    })
  },
  // 提交案理表单
  submitForm(e) {
    var that = this
    wx.showModal({
      title: '提交',
      content: '确定提交该案例吗？',
      success: function(res) {
        if (res.confirm) {
          // 在这验证数据非空和发送请求到后台
          console.log(that.data)
          // console.log(JSON.stringify(that.data.pAddress))
          var locate = JSON.stringify(that.data.pAddress)
          var note = that.data.remarks
          var telephone = that.data.pPhone
          var age = that.data.pAge
          var level = that.data.condition
          var disease_type = that.data.illness
          var gender = that.data.pSex
          var name = that.data.patientName
          var doctortokenid = that.data.doctorTokenId
          var caseId = that.data.caseId
          var doctorToken = app.globalData.doctorToken
          // 在这验证数据非空和发送请求到后台
          // console.log('上报的患者信息', locate, note, telephone, age, level, disease_type, gender, name)
          if (locate != '' && telephone != '' && age != '' && disease_type != '' && name != '' && gender != '') {
            wx.request({
              url: modifyCase_url,
              header: {
                'Authorization': doctorToken
              },
              method: 'POST',
              data: {
                locate: locate,
                note: note,
                telephone: telephone,
                age: age,
                level: level,
                diseaseType: disease_type,
                gender: gender,
                name: name,
                doctorTokenId: doctortokenid,
                caseId: caseId
              },
              success(res) {
                console.log('提交成功', res)
                if (res.data.code == 200) {
                  wx.showToast({
                    title: '提交成功，等待审核!',
                    icon: 'none'
                  })
                }
              }
            })
            // 修改后返回
            setTimeout(that.giveup,500) 
          }
        }
      }
    })
  },
  //放弃修改
  giveup(e) {
    wx.navigateBack({

    })
  },
  // input框监听函数
  inputLine(e) {
    var inputLine = e.currentTarget.dataset.id
    var str = e.detail.value
    this.setData({
      [inputLine]: str
    })
  },
  //清空一行
  emptyLine(e) {
    console.log(e.currentTarget)
    var inputLine = e.currentTarget.id
    this.setData({
      [inputLine]: ''
    })
  },
  // onLoad函数
  onLoad(e) {
    var that = this
    var case_data = JSON.parse(e.case_data)
    console.log(case_data)
    // for (var i=0; i < that.data.degreeArr.length;i++){
    //   if (case_data.degree == that.data.degreeArr[i]){
    //     var degreeId=i
    //   }
    // }

    var conditionId = that.data.conditionArr.indexOf(case_data.condition)

    // console.log(case_data)
    this.setData({
      patientName: case_data.patientName, //病人姓名
      pAddress: case_data.pAddress, //通讯地址
      pAge: case_data.pAge, //年龄
      pSex: case_data.pSex, //性别
      pPhone: case_data.pPhone, //联系方式
      // degree: case_data.degree, //病情程度
      // degreeId:degreeId,
      condition: case_data.condition, //当前状态
      conditionId: conditionId,
      illness: case_data.illness, //疾病类型
      // illDate: case_data.illDate, //发病时间
      // symptom: case_data.symptom, //发病症状
      remarks: case_data.remarks, //备注信息
      caseId: case_data.caseid,
      doctorTokenId: case_data.doctortokenid
    })
  }

})